package com.ksd.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.domain.entity.SysUser;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.framework.web.domain.server.Sys;
import com.ksd.quartz.domain.SysJob;
import com.ksd.quartz.service.ISysJobService;
import com.ksd.quartz.util.TaskConstans;
import com.ksd.system.service.ISysOperLogService;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.service.ISysBoardService;

/**
 * 板卡配置Controller
 * 
 * @author y.wu
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/system/board")
public class SysBoardController extends BaseController
{
    @Autowired
    private ISysBoardService sysBoardService;
    @Autowired
    private ISysOperLogService  sysOperLogService;
    @Autowired
    private ISysJobService sysJobService;

    /**
     * 查询板卡配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:board:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBoard sysBoard)
    {
        startPage();
        List<SysBoard> list = sysBoardService.selectSysBoardList(sysBoard);
//        for (SysBoard board : list) {
//            Long id = board.getId();
//            if (id == SysBoard.cloudSynchFlag){
//                String url = board.getUrl();
//                if (url.equals("0")){
//                    board.setUrl("开启");
//                }else if (url.equals("1")){
//                    board.setUrl("关闭");
//                }
//            }
//        }
        return getDataTable(list);
    }

    /**
     * 导出板卡配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:board:export')")
    @Log(title = "板卡配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBoard sysBoard)
    {
        List<SysBoard> list = sysBoardService.selectSysBoardList(sysBoard);
        ExcelUtil<SysBoard> util = new ExcelUtil<SysBoard>(SysBoard.class);
        util.exportExcel(response, list, "板卡配置数据");
    }

    /**
     * 获取板卡配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:board:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysBoardService.selectSysBoardById(id));
    }

    /**
     * 新增板卡配置
     */
    @PreAuthorize("@ss.hasPermi('system:board:add')")
    @Log(title = "板卡配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBoard sysBoard)
    {
        return toAjax(sysBoardService.insertSysBoard(sysBoard));
    }

    /**
     * 修改板卡配置
     */
    @PreAuthorize("@ss.hasPermi('system:board:edit')")
    @Log(title = "板卡配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBoard sysBoard) throws SchedulerException {
        if (SysBoard.serviceFileId !=  sysBoard.getId()){
            if(!sysBoardService.checkUrlUnique(sysBoard)){
                sysOperLogService.insertOperlogError("修改直播板卡" + sysBoard.getId() + "请求地址失败，该请求地址已存在");
                return error("修改板卡" + sysBoard.getId()+ "请求地址失败，该请求地址已存在");
            }
        }
//        if(SysBoard.cloudSynchFlag == sysBoard.getId()){
//            //判断同步开关,关闭版本升级
//            SysJob sysJobApk = sysJobService.selectJobById(SysJob.updateApkTaskId);
//            SysJob sysJobJar = sysJobService.selectJobById(SysJob.updateJarTaskId);
//            SysJob sysJobVue = sysJobService.selectJobById(SysJob.updateVueTaskId);
//                //0 开启 1关闭与job状态一致
//            sysJobApk.setStatus(sysBoard.getUrl());
//                //关闭
//            sysJobJar.setStatus(sysBoard.getUrl());
//            sysJobVue.setStatus(sysBoard.getUrl());
//            sysJobService.changeStatus(sysJobApk);
//            sysJobService.changeStatus(sysJobJar);
//            sysJobService.changeStatus(sysJobVue);
//        }

        String url = sysBoard.getUrl();
        Long id = sysBoard.getId();
        SysBoard sysBoardTemp = sysBoardService.selectSysBoardById(id);
        sysOperLogService.insertOperlogSuccess("修改板卡"+id+"的请求地址成功.原地址: "+sysBoardTemp.getUrl()+" ,更新地址为: "+url);


        return toAjax(sysBoardService.updateSysBoard(sysBoard));
    }



    /**
     * 删除板卡配置
     */
    @PreAuthorize("@ss.hasPermi('system:board:remove')")
    @Log(title = "板卡配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysBoardService.deleteSysBoardByIds(ids));
    }
}
